package com.skywolf.chem.manager.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.skywolf.chem.article.entity.Position;
import com.skywolf.chem.common.context.LoginUser;
import com.skywolf.chem.common.context.RequestContext;
import com.skywolf.chem.common.enums.ResponseCodeEnum;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.common.sysconfig.response.RespData;
import com.skywolf.chem.manager.entity.ColumnQueryParam;
import com.skywolf.chem.manager.entity.ColumnVo;
import com.skywolf.chem.pageconfig.entity.ColumnInfo;
import com.skywolf.chem.pageconfig.service.IColumnInfoService;
import com.skywolf.chem.usercenterapi.service.IPositionService;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 栏目表 前端控制器
 * </p>
 *
 * @author 
 * @since 2020-06-09
 */
@RestController
@RequestMapping("/manager/column")
public class ColumnInfoManagerController {

    @Autowired
    IColumnInfoService columnInfoService;

    @Autowired
    IPositionService positionService;

    @ApiOperation(value = "获取市州栏目",notes = "获取市州栏目")
    @RequestMapping(value = "/getCityColumns",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<ColumnInfo> getCityColumns() {
        List<ColumnInfo> list = columnInfoService.getAllCity();
        ColumnInfo columnInfo = new ColumnInfo();
        columnInfo.setColName("省局账号");
        columnInfo.setId(0L);
        list.add(0,columnInfo);
        return RespData.success(list);
    }

    @ApiOperation(value = "获取栏目",notes = "获取栏目，first_page-首页 live_page-直播页")
    @RequestMapping(value = "/getColumns",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<ColumnInfo> getColumns(@RequestBody @Validated ColumnQueryParam queryParam) {
        IPage<ColumnInfo> page = columnInfoService.ColumnPageList(queryParam);
        PageVO<ColumnInfo> pageVO = new PageVO<>(page);
        return RespData.success(pageVO);
    }

    @ApiOperation(value = "获取用户所属的市州栏目",notes = "获取用户所属的市州栏目")
    @RequestMapping(value = "/getUserColumns",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<List<ColumnInfo>> getUserColumns() {
        LoginUser user = RequestContext.getLoginUser();
        List<ColumnInfo> list = new ArrayList<>();
        if(user.getCity()!=0){
            //地市州管理人员
            ColumnInfo columnInfo = columnInfoService.getById(user.getCity());
            list.add(columnInfo);
        }else{
            list = columnInfoService.list();
        }
        return RespData.success(list);
    }


    @ApiOperation(value = "添加栏目",notes = "添加栏目")
    @RequestMapping(value = "/addColumn",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> addColumn(@RequestBody @Validated ColumnInfo columnInfo) {
        List<ColumnInfo> list = columnInfoService.list(new QueryWrapper<ColumnInfo>()
                .eq("col_name",columnInfo.getColName()).eq("position_name",columnInfo.getPositionName()));
        if(list!=null && list.size()>0){
            return RespData.failure(ResponseCodeEnum.COLUMNS_ERROR);
        }
        columnInfoService.save(columnInfo);
        return RespData.success("success");
    }

    @ApiOperation(value = "删除栏目",notes = "删除栏目")
    @RequestMapping(value = "/delColumn",method = {RequestMethod.GET},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> delColumn(@RequestParam(value = "id",required = true) Long id) {
        List<Position> list = positionService.list(new QueryWrapper<Position>()
                .eq("column_id",id));
        if(list!=null && list.size()>0){
            return RespData.failure(ResponseCodeEnum.COLUMNS_ARTICLE_ERROR);
        }
        columnInfoService.removeById(id);
        return RespData.success("success");
    }


    @ApiOperation(value = "更新栏目",notes = "更新栏目")
    @RequestMapping(value = "/updateColumn",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> updateColumn(@RequestBody @Validated ColumnInfo columnInfo) {
        columnInfoService.updateById(columnInfo);
        return RespData.success("success");
    }

}
